package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.Employees;
import com.tenton.service.EmployeesService;
import com.tenton.utils.Constants;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("员工管理")
@RequestMapping("/employee")
public class EmployeesController {
    private Logger logger = Logger.getLogger(EmployeesController.class);
    @Autowired
    EmployeesService employeesService;

    /**
     * 登录界面
     *
     * @return
     */
    @ApiOperation("员工登录界面")
    @GetMapping("/login")
    public String login() {
        logger.debug("LoginController welcome AppInfoSystem develpor==================");
        return "employee/employeeLogin";
    }

    /**
     * 登录验证
     *
     * @param empName
     * @param pwd
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("登录验证")
    @PostMapping(value = "/doLogin")
    public String doLogin(@RequestParam String empName, @RequestParam String pwd,
                          HttpSession session, HttpServletRequest request) {
        boolean flag = employeesService.getLogin(empName, pwd, session);
        if (flag) {
            return "forward:/employee/flatform/main";
        } else {
            request.setAttribute("error", "账号或密码不正确");
            return "employee/employeeLogin";
        }
    }

    @PostMapping(value = "/flatform/main")
    public String main(HttpSession session) {
//		验证是否有session信息，防止非法登录，没有就跳转到登录页面
        if (session.getAttribute(Constants.EMPLOYEE_SESSION) == null) {
            return "redirect:/employee/login";
        }
        return "employee/main";
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping(value = "/logout")
    public String logout(HttpSession session) {
        //使Session失效
        session.invalidate();
        return "employee/employeeLogin";

    }

    /**
     * 获取员工信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("通过Id获取员工信息")
    @GetMapping("/getEmployee")
    public String getEmployee(Integer id, Model model) {
        employeesService.getEmployeesById(id, model);
        return "employee/getEmployee";

    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改页面")
    @GetMapping("/toUpdate")
    public String editEmp(Integer id, Model model) {
        employeesService.getEmployeesById(id, model);
        return "employee/editEmployee";
    }

    /**
     * 修改员工信息
     *
     * @param employees
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("修改员工信息")
    @PostMapping("/update")
    public String update(Employees employees, HttpSession session, HttpServletRequest request) {
        //使Session失效
        //session.invalidate();
        try {
            employeesService.update(employees,session);
            return "redirect:/employee/login";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "员工信息为空！");
            return "redirect:/employee/toUpdate";
        }
    }

    /**
     * 员工姓名模糊查询
     *
     * @param empName
     * @param pn
     * @param model
     * @return
     */
    @ApiOperation("员工姓名模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(@ApiParam("员工姓名") @RequestParam(value = "empName", required = false) String empName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn,
                              ModelMap model) {
        employeesService.getByParams(empName, pn, model);
        return "admin/employees/getEmployeesByParams";
    }
}
